import 'package:get/get.dart';
import 'package:share_fit/app/controllers/base/base_controller.dart';
import 'package:share_fit/app/helper/ext/tr_ext.dart';
import 'package:share_fit/app/tools/log_utils.dart';
import 'package:share_fit/app/ute/uteped_const.dart';
import 'package:share_fit/app/ute/uteped_device.dart';
import 'package:share_fit/generated/locales.g.dart';

import 'quick_reply_state.dart';

class QuickReplyLogic extends BaseController {
  final QuickReplyState state = QuickReplyState();

  void setSmsReplyEnable(bool value) {
    showLoading();
    state.smsReplyEnable = value;
    bRemoteReqAsync(UTEPedConst.methodSetSmsReplyEnable, {UTEPedConst.keyEnable: value})
        .then((success) => _onSetSmsRemindEnable(success, value));
  }

  void _onSetSmsRemindEnable(success, value) {
    cancelLoading();
    if (success) {
      save(UTEPedConst.methodSetSmsReplyEnable, value);
      update();
      if (state.smsReplyEnable) {
        syncSmsReplyContent();
      }
    }
    else {
      state.smsReplyEnable = !value;
    }
  }

  void syncSmsReplyContent() {
    showLoading();
    List syncReplyList = [];
    syncReplyList.addAll(state.fixedReplyList);
    if (state.replyList.isNotEmpty) {
      syncReplyList.addAll(state.replyList);
    }
    dRemoteReqAsync(UTEPedConst.methodSyncSmsReplyContent, {UTEPedConst.typeList: syncReplyList})
        .then((value) => {
              if (value[UTEPedConst.keySuccess]) {
                if (state.replyList.isEmpty) {
                  cancelLoading(), update()
                }
              }
              else {
                cancelLoading(),
                if (UTEPedConst.errorCode408 == value[UTEPedConst.keyErrorCode]) {
                  showTips(LocaleKeys.info.ts,LocaleKeys.device_is_busy.ts),
                  UTEPedDevice.instance.isDeviceBusy().then((value) => logger.d("device busy:$value"))
                }
                else {
                  showTips(LocaleKeys.info.ts,LocaleKeys.operation_failed.ts)
                }
              }
            });
  }

  void addReply(String text) {
    if (text.isEmpty) {
      return;
    }
    if (state.replyList.contains(text)) {
      showTips(LocaleKeys.info.ts, LocaleKeys.quick_reply_exist.ts);
      return;
    }
    state.replyList.add(text);
    syncSmsReplyContent();
  }

  void replaceReply(String oldContent, String newContent) {
    if (oldContent == newContent) {
      return;
    }

    state.replyList.remove(oldContent);
    state.replyList.add(newContent);
    syncSmsReplyContent();
  }

  void deleteReply(String text) {
    if (state.replyList.contains(text)) {
      state.replyList.remove(text);
      syncSmsReplyContent();
    }
  }

  @override
  String getRemotePath() {
    return UTEPedConst.pathContact;
  }

  @override
  String getStoragePath() {
    return UTEPedConst.pathDevice;
  }

  @override
  void initState() {
    state.fixedReplyList.add(LocaleKeys.quick_reply_message_1.ts);
    state.fixedReplyList.add(LocaleKeys.quick_reply_message_2.ts);
    state.fixedReplyList.add(LocaleKeys.quick_reply_message_3.ts);
    state.fixedReplyList.add(LocaleKeys.quick_reply_message_4.ts);
    state.fixedReplyList.add(LocaleKeys.quick_reply_message_5.ts);

    state.replyList = read(UTEPedConst.methodSyncSmsReplyContent, defaultValue: []);
    state.smsReplyEnable = read(UTEPedConst.methodSetSmsReplyEnable, defaultValue: false);

    dRemoteReqAsync(UTEPedConst.methodSmsReplyContentCount, {})
        .then((value) => state.maxReplyCount = value);
  }

  @override
  void onRemoteEvent(event, method) {
    if (UTEPedConst.methodSyncSmsReplyContent == method) {
      var action = event[UTEPedConst.keyAction];
      var value = event[UTEPedConst.keyValue];
      /// int START = 1; int FINISH = 2;int FAIL = 3;
      if (UTEPedConst.actionSyncSmsReplyState == action) {
        if (value == UTEPedConst.syncStateFail) {
          cancelLoading();
          showTips(LocaleKeys.info.ts,LocaleKeys.operation_failed.ts);
        }
        else if (value == UTEPedConst.syncStateFinish) {
          cancelLoading();
          if (state.replyList.isNotEmpty) {
            save(UTEPedConst.methodSyncSmsReplyContent, state.replyList);
            update();
          }
        }
      }
      else if (UTEPedConst.actionSyncSmsReplyProgress == action) {
        logger.d("syn reply progress $value");
      }
    }
  }

}
